SQL基础3-面试题

1、编写下列SQL 有两张表
Table1,班级表class;字段:班级编号class_id ,班级名称class_name
Table2,学生表student;字段:学生学号stu_id,学生姓名stu_name,班级编号cls_id,期末六门总成绩exam_total_score
根据下列题目写出sql:
求每个班级中的最高分数和最低分数,并且它的最小值小于200,最大值大于400。

使用where:在磁盘读入数据时就进行判断,因此要使用原始字段名

select MIN(total_score), stu_id, stu_name, cls_id from student where total_score<200 GROUP BY cls_id

select MAX(total_score), stu_id, stu_name, cls_id from student where total_score>400 GROUP BY cls_id

使用having:读入内存后才开始判断,可以使用max(total_score)或者别名

select max(total_score), stu_id, stu_name, cls_id from student  GROUP BY cls_id HAVING max(total_score)>400

slect MIN(total_score), stu_id, stu_name, cls_id from student  GROUP BY cls_id HAVING MIN(total_score)<200

select MAX(total_score) as a, stu_id, stu_name, cls_id from student  GROUP BY cls_id

内连接查询两张表

select MAX(total_score) as a, stu_id, stu_name, cls_id, class.class_name from student INNER JOIN class where cls_id=class.class_id  GROUP BY cls_id HAVING a>400

2.编写SQL语句 创建一张学生表,包含以下信息,学号,姓名,年龄,性别,家庭住址,联系电话

Create table stu (学号 int not null primary key,
姓名 varchar(8),
年龄 int,
性别 varchar(4),
家庭地址 varchar(50),
联系电话 int
);

3. 用一条SQL 语句 查询出每门课都大于80 分的学生姓名
name kecheng fenshu 
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90

 select distinct name from table where name not in (select distinct name from table where fenshu<=80);

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值